﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace MoneyBook.Interactivity {
    public sealed class InvokeParameter : ParameterBase {

        public string PropertyName {
            get { return (string)GetValue(PropertyNameProperty); }
            set { SetValue(PropertyNameProperty, value); }
        }

        public static readonly DependencyProperty PropertyNameProperty =
            DependencyProperty.Register("PropertyName", typeof(string), typeof(InvokeParameter), null);

        public override object GetParameterValue(object invokeParameter) {
            object result = invokeParameter;
            if (!string.IsNullOrWhiteSpace(this.PropertyName) && invokeParameter != null) {
                PropertyInfo prop = invokeParameter.GetType().GetProperty(this.PropertyName, BindingFlags.Public | BindingFlags.Instance);
                if (prop == null) {
                    //TODO Message
                    throw new InvalidOperationException();
                }
                if (!prop.CanRead) {
                    //TODO Message
                    throw new InvalidOperationException();
                }
                result = prop.GetValue(invokeParameter, null);
            }
            return result;
        }  
    }
}
